#include "mfUtils.h"

uint64_t startTimeMillis = 0;

void ofLaunchBrowser(string url)
{
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithCString:url.c_str()]]];
}

void mfDialNumber(string number)
{
	number = "tel://"+number;
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithCString:number.c_str()]]];
}

//--------------------------------------------------
string ofToString(double value, int precision){
	stringstream sstr;
	sstr << fixed << setprecision(precision) << value;
	return sstr.str();
}

//--------------------------------------------------
string ofToString(int value){
	stringstream sstr;
	sstr << value;
	return sstr.str();
}



//--------------------------------------------------
int ofGetSeconds(){
	time_t 	curr;
	tm 		local;
	time(&curr);
	local	=*(localtime(&curr)); 
	return local.tm_sec; 
}

//--------------------------------------------------
int ofGetMinutes(){
	time_t 	curr;
	tm 		local;
	time(&curr);
	local	=*(localtime(&curr)); 
	return local.tm_min; 
}

//--------------------------------------------------
int ofGetHours(){
	time_t 	curr;
	tm 		local;
	time(&curr);
	local	=*(localtime(&curr)); 
	return local.tm_hour; 
}



//--------------------------------------------------
int ofGetFrameNum(){
	return numFrames;
}

//--------------------------------------------------
int ofGetElapsedTimeMillis()
{
	// just a sanity check
	if( startTimeMillis==0 )
		setStartTime();
	
	// If this is the first time we've run, get the timebase.
    // We can use denom == 0 to indicate that sTimebaseInfo is
    // uninitialised because it makes no sense to have a zero
    // denominator is a fraction.
	static mach_timebase_info_data_t    sTimebaseInfo;
    if ( sTimebaseInfo.denom == 0 ) {
        (void) mach_timebase_info(&sTimebaseInfo);
    }
	
    // Calculate the duration.
    uint64_t elapsed = mach_absolute_time() - startTimeMillis;
	
    // Convert to nanoseconds.
    // Do the maths.  We hope that the multiplication doesn't
    // overflow; the price you pay for working in fixed point.
	uint64_t elapsedNano = elapsed * sTimebaseInfo.numer / sTimebaseInfo.denom;
	
	// convert to milliseconds and return as an int
    return (int) (elapsedNano / 1000000);
}

//--------------------------------------------------
float ofGetElapsedTimef()
{
	return (float)ofGetElapsedTimeMillis()/1000.0f;
}



//==================================================
//--------------------------------------------------
// not for the public, just for glue -> util comm.
void setFrameNum(int num){
	numFrames = num;
}

//--------------------------------------------------
// not for the public, just for glue -> util comm.
void setStartTime(){
	startTimeMillis = mach_absolute_time();
}